package com.example.xinli.controller;

import com.example.xinli.domain.dto.imgDTO;
import com.example.xinli.domain.vo.GetUserInfoVo;
import com.example.xinli.service.IUserService;
import com.example.xinli.domain.vo.ResponseBean;
import com.example.xinli.utils.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    // 根据获取所有用户信息
    @RequestMapping("/getAllUser")
    @ResponseBody
    public ResponseBean getAllUserInfo() {
        return userService.getAllUserInfo();
    }

    @RequestMapping("/getPreUser")
    @ResponseBody
    public ResponseBean getPreUserInfo() {
        return userService.getPreUserInfo();
    }
    // 根据token获取用户信息
    @RequestMapping("/getUserInfo")
    @ResponseBody
    public ResponseBean getUserInfo(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        token = token.split(" ")[1];
        return userService.getUserInfo(token);
    }

    // 获取用户进行干预的记录
    @RequestMapping("/getUserRecords")
    @ResponseBody
    public ResponseBean getUserRecords(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        token = token.split(" ")[1];
        return userService.getUserRecords(token);
    }

    @RequestMapping("/evalRole")
    @ResponseBody
    public ResponseBean evalRole(@RequestParam("key") String key) {

        return userService.evalRole(key);
    }

    @RequestMapping("/setRole")
    @ResponseBody
    public ResponseBean setRole(HttpServletRequest request,@RequestParam("role") String role) {
        String token = request.getHeader("Authorization");
        token = token.split(" ")[1];
        String userid= TokenUtil.getId(token);
        return userService.setRole(userid,role);
    }

    @RequestMapping("/setRoleKey")
    @ResponseBody
    public ResponseBean setRoleKey(@RequestParam("key") String key) {
        return userService.setevalKey(key);
    }


    @RequestMapping("/setAvatar")
    @ResponseBody
    public ResponseBean setAvatar(HttpServletRequest request,@RequestBody imgDTO imgDTO) {
        String token = request.getHeader("Authorization");
        token = token.split(" ")[1];
        String userid= TokenUtil.getId(token);

        return userService.setUserAvatar(userid,imgDTO.getUrl());
    }

    @RequestMapping("/setName")
    @ResponseBody
    public ResponseBean setName(HttpServletRequest request,@RequestParam("name") String name) {

        String token = request.getHeader("Authorization");
        token = token.split(" ")[1];
        String userid= TokenUtil.getId(token);

        return userService.setUserName(userid,name);
    }

    //时间到了，解锁下一个单元
//    @RequestMapping("/unlock")
//    @ResponseBody
//    public ResponseBean getUserRecords(HttpServletRequest request) {
//        String token = request.getHeader("Authorization");
//        token = token.split(" ")[1];
//        return userService.getUserRecords(token);
//    }
}

